package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.AttrGroupRelationVo;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrsVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationService attrgroupRelationService;
    @Autowired
    private AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        LambdaQueryWrapper<AttrGroupEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (!StringUtils.isEmpty(key)) {
            lambdaQueryWrapper.and(wrapper -> wrapper.eq(AttrGroupEntity::getAttrGroupId, key)
                    .or().
                    like(AttrGroupEntity::getAttrGroupName, key));

        }
        if (catelogId == 0) {
            //查询全部
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), lambdaQueryWrapper);
            return new PageUtils(page);
        } else {
            lambdaQueryWrapper.eq(AttrGroupEntity::getCatelogId, catelogId);

            return new PageUtils(this.page(new Query<AttrGroupEntity>().getPage(params), lambdaQueryWrapper));

        }
    }

    @Override
    public List<AttrGroupWithAttrsVo> CatelogWithAttr(Long catelogId) {
        //查询分组


        List<AttrGroupEntity> attrGroupEntityList = this.list(new LambdaQueryWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getCatelogId, catelogId));
        //通过组id找到属性
        List<AttrGroupWithAttrsVo> collect = attrGroupEntityList.stream().map((item) -> {
            //获取分组信息
//            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
//            attrGroupWithAttrsVo.setAttrGroupId(item.getAttrGroupId());
//            attrGroupWithAttrsVo.setAttrGroupName(item.getAttrGroupName());
//            attrGroupWithAttrsVo.setCatelogId(item.getCatelogId());
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(item, attrGroupWithAttrsVo);
            attrGroupWithAttrsVo.setCatelogId(catelogId);


            List<AttrEntity> attrEntities = attrService.AttrRelation(attrGroupWithAttrsVo.getAttrGroupId());
            attrGroupWithAttrsVo.setAttrs(attrEntities);

            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());


        return collect;
    }
}